package com.yztc.yidoutang.activity.register_activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.yztc.yidoutang.R;
import com.yztc.yidoutang.base.BaseActivity;

/**
 * 注册界面
 */
public class RegisterActivity extends BaseActivity implements RegisterContract.View {
    private EditText phone;
    private EditText code;
    private EditText psw;
    private Button postCode;
    private CheckBox showPsw;
    private Button register;
    private Button back;

    private RegisterPresenter presenter = new RegisterPresenter(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        phone = (EditText) findViewById(R.id.find_register_et_phone);
        code = (EditText) findViewById(R.id.find_register_et_code);
        psw = (EditText) findViewById(R.id.find_register_et_psw);
        postCode = (Button) findViewById(R.id.find_register_btn_postcode);
        showPsw = (CheckBox) findViewById(R.id.find_register_cb_showpsw);
        register = (Button) findViewById(R.id.find_register_btn_register);
        back = (Button) findViewById(R.id.find_register_btn_back);
        initEvents();
    }

    /**
     * 初始化控件的交互事件
     */
    private void initEvents() {
        // 手机号码输入框的文本变更事件
        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                onEditTextTextChanged("phone", s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        // 验证码输入框的文本变更事件
        code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                onEditTextTextChanged("code", s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        // 密码输入框的文本变更事件
        psw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                onEditTextTextChanged("psw", s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        // 获取验证码按钮的点击事件
        postCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onPostCodeClicked();
            }
        });
        // 隐藏/显示密码按钮的点击事件
        showPsw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                onShowHidePswClicked(isChecked);
            }
        });
        // 注册按钮的点击事件
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRegisterClicked();
            }
        });
        // 返回按钮的点击事件
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackClicked();
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void onEditTextTextChanged(String type, String text) {
        presenter.onEditTextTextChanged(RegisterActivity.this, type, text, register);
    }

    @Override
    public void onPostCodeClicked() {
        presenter.onPostCodeClicked(RegisterActivity.this, phone, postCode);
    }

    @Override
    public void onShowHidePswClicked(boolean isChecked) {
        presenter.onShowHidePswClicked(psw, isChecked);
    }

    @Override
    public void onRegisterClicked() {
        presenter.onRegisterClicked(phone, code, psw);
    }

    @Override
    public void onBackClicked() {
        presenter.onBackClicked(RegisterActivity.this);
    }
}